//
//  HJApplyListController.m
//  KaiChat
//
//  Created by lx on 2023/10/21.
//

#import "HJApplyListController.h"
#import "HJIM.h"
#import "HJApplyListCell.h"
#import "HJSectionHeaderView.h"
#import "HJChatController.h"

@interface HJApplyListController ()

@property (nonatomic, strong) NSArray *datas;

@end

@implementation HJApplyListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"New Friends";
    self.view.backgroundColor = UIColor.tj_F6F6F6Color;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    [self.tableView registerClass:HJApplyListCell.class forCellReuseIdentifier:@"HJApplyListCell"];
    [self.tableView registerClass:HJSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"HJSectionHeaderView"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 70;
    self.tableView.backgroundColor = UIColor.tj_F6F6F6Color;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    [HJIM.shareIM.userManager loadUserApplyListDataComplete:^(NSArray<NSArray<HJUserApplyModel *> *> * _Nullable datas) {
        self.datas = datas;
        [self.tableView reloadData];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *datas = self.datas[section];
    return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJApplyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJApplyListCell" forIndexPath:indexPath];
    HJUserApplyModel *model = self.datas[indexPath.section][indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setDidClickHelloButtonBlock:^{
        //加别人,别人已经接受了,
        HJChatSessionModel *sessionModel = [[HJChatSessionModel alloc] init];
        sessionModel.sessionId = toString(model.sessionId);
        sessionModel.chatType = HJChatTypeSingle;
        sessionModel.receiveId = model.userId;
        sessionModel.uid = model.userId;
        sessionModel.sessionName = model.nickname;
        [weakself.navigationController pushViewController:[HJChatController chatControllerWithSessionModel:sessionModel]];
    }];
    
    [cell setDidClickWaitingButtonBlock:^{
        if (model.way.intValue == 2) {//接受
            [HJHUD showLoadingInView:weakself.view];
            [HJIM.shareIM.userManager handleFriendAddWithUserId:model.userId accept:YES complete:^(NSString * _Nullable sessionId) {
                if (!isEmptyString(sessionId)) {
                    model.status = @(1);
                    [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                }
            }];
        }
    }];
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     NSArray *sectionDatas = self.datas[section];
    return sectionDatas.count == 0 ? CGFLOAT_MIN : 20;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSArray *sectionDatas = self.datas[section];
    HJSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HJSectionHeaderView"];
    if (section == 0) {
        view.titleLabel.text = sectionDatas.count == 0 ? nil : @"Within 3 Days";
    } else {
        view.titleLabel.text = sectionDatas.count == 0 ? nil : @"3 Days ago";
    }
    view.contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    view.backgroundColor = UIColor.tj_F6F6F6Color;
    view.titleLabel.textColor = UIColor.tj_BEBDCAColor;
    view.titleLabel.font = UIFontMake(12);
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
